package com.xpec.c4.game.utility;

import java.util.ArrayList;
import java.util.Collections;

public class DrawLotsUtility {
	
	private static final int THANKS_FOR_PATRONAGE = 0;
	
	private static final int WINNING_STICKS = 1;
	
	private ArrayList<Integer> ballotBox = new ArrayList<Integer>();

	public DrawLotsUtility(int totalSticks, int winningSticks) {
		initData(totalSticks, winningSticks);
	}
	
	
	/**
	 * 搖晃籤筒，把裡面得籤打散
	 * 
	 */
	public void shakeBalloBox() {
		Collections.shuffle(ballotBox);
	}
	
	
	/**
	 * 搖晃籤筒，把裡面得籤打散
	 * 
	 * @param count 次數
	 * @return
	 */
	public void shakeBalloBox(int count) {
		for (int i = 0; i < count; i++) {
			Collections.shuffle(ballotBox);
		}
	}
	
	/**
	 * 抽獎囉
	 * 
	 * @return
	 */
	public boolean luckyDraw() {
		int luckyNum = GameUtility.randomInt(0, ballotBox.size() - 1);
		boolean isGet = ballotBox.get(luckyNum) == WINNING_STICKS ? true : false;
		return isGet;
	}
	
	/**
	 * 重新設定籤筒
	 * @param totalSticks
	 * @param winningSticks
	 */
	public void setBallotBox(int totalSticks, int winningSticks) {
		ballotBox = new ArrayList<Integer>();
		
		initData(totalSticks, winningSticks);
	}
	
	private void initData(int totalSticks, int winningSticks){
		// 將籤塞入籤筒
		for (int count = 0; count < totalSticks; count++) {
			if (count < winningSticks) {
				ballotBox.add(WINNING_STICKS);
			} else {
				ballotBox.add(THANKS_FOR_PATRONAGE);
			}
		}

		// 直接打亂一次
		Collections.shuffle(ballotBox);
	}
}
